package com.bamboo.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果
 * @author jack.wu
 */
@Data
public class ResponseEntity {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private int code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();


    /**
     * 成功静态方法
     */
    public static ResponseEntity ok() {
        ResponseEntity response = new ResponseEntity();
        response.setSuccess(true);
        response.setCode(ResultCode.SUCCESS);
        response.setMessage("成功");
        return response;
    }

    /**
     * 失败静态方法
     */
    public static ResponseEntity error() {
        ResponseEntity response = new ResponseEntity();
        response.setSuccess(false);
        response.setCode(ResultCode.ERROR);
        response.setMessage("失败");
        return response;
    }


    public ResponseEntity success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public ResponseEntity message(String message){
        this.setMessage(message);
        return this;
    }

    public ResponseEntity code(int code){
        this.setCode(code);
        return this;
    }

    public ResponseEntity data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public ResponseEntity data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

    private ResponseEntity() {}

}
